package com.perye.dokit.weController;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.exception.BadRequestException;
import com.perye.dokit.service.DictDetailService;
import com.perye.dokit.service.PersonnelService;
import com.perye.dokit.service.WeWeddingStyleService;
import com.perye.dokit.utils.ArrayAssembly;
import com.perye.dokit.utils.DateUtils;
import com.perye.dokit.utils.UploadFile;
import com.perye.dokit.utils.pageData.PageData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName PersonnelController
 * @Description 人员管理类
 * @Date 2022/3/17 14:06
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/personnel")
public class PersonnelController extends BaseController {

    @Autowired
    private PersonnelService personnelService;

    @Autowired
    private DictDetailService dictDetailService;

    @Value("${uploadFilePath}")
    String uploadFilePath;

    @Value("${uploadFileMapping}")
    String uploadFileMapping;

    @Log("查询人员列表")
    @ApiOperation("查询人员列表")
    @GetMapping()
    @PreAuthorize("@dokit.check('personnel:list')")
    public ResponseEntity<Object> personnelList() {
        PageData pageDataList = null;
        try {
            PageData pageData = this.getPageData();
            int page = Integer.parseInt(pageData.getString("page")) + 1;
            PageHelper.startPage(page, Integer.parseInt(pageData.getString("size")));

            List<PageData> deptDtos = personnelService.queryPersonnelList(pageData);


            PageInfo<PageData> pageInfo = new PageInfo<PageData>(deptDtos);

            int count = (int) pageInfo.getTotal();
            pageDataList = ArrayAssembly.pageList(deptDtos, count);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return new ResponseEntity<>(pageDataList, HttpStatus.OK);
    }

    @Log("删除人员")
    @ApiOperation("删除人员")
    @PreAuthorize("@dokit.check('personnel:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) throws Exception {

        personnelService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("新增人员")
    @ApiOperation("新增人员")
    @PostMapping
    @PreAuthorize("@dokit.check('personnel:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PageData pageData) throws Exception {

        int num = personnelService.create(pageData);
        if (num <= 0) {
            throw new BadRequestException("新增失败");
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改人员")
    @ApiOperation("修改人员")
    @PutMapping
    @PreAuthorize("@dokit.check('personnel:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PageData pageData) throws Exception {

        personnelService.update(pageData);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @RequestMapping("/updateIsOpenState")
    @ResponseBody
    public PageData updateIsOpenState(@Validated @RequestBody PageData pageData) {
        PageData result = new PageData();
        try {
            personnelService.update(pageData);
            result.put("code", "00");
            result.put("msg", "修改成功");
            result.put("data", null);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "01");
            result.put("msg", "修改失败");
            result.put("data", null);
        }
        return result;
    }


    @RequestMapping("/uploadFile")
    @ResponseBody
    public PageData uploadFile(@RequestParam("file") MultipartFile file) {
        //操作MultipartFile uploadImage, String moduleName
        //返回结果
        PageData result = new PageData();
        //参数  uploadFilePath 上传文件路径//uploadFileMapping 映射路径

        try {
            String filePath = UploadFile.uploadFile(file, uploadFilePath, uploadFileMapping);

            result.put("code", "00");
            result.put("filePath", filePath);
            result.put("msg", "上传成功");
        } catch (Exception e) {

        }
        return result;
    }

    @RequestMapping("/queryPersonnelTypeList")
    @ResponseBody
    public PageData queryPersonnelTypeList() {

        PageData result = new PageData();

        List<PageData> personnelTypeList = dictDetailService.queryDictDetailByName("personnel_type", "list");

        result.put("code", "00");
        result.put("msg", "查询成功");
        result.put("data", personnelTypeList);
        return result;
    }

}
